package joodle.ui;

import static joodle.si.ResourceManager.message;

import java.awt.Dimension;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import joodle.domain.Canvas;
import joodle.domain.Shape;
import joodle.tools.SelectionObserver;

/**
 * The entry point of the application and container of the Canvas representation, CanvasComponent, 
 * this class is responsible for using the services of other layers to accomplish user goals.
 */
@SuppressWarnings("serial")
public class JoodleFrame extends JFrame {

	static String GUIDE_URL = "http://www.ug.bcc.bilkent.edu.tr/~m_ozdal/Site/release/guide/guide.htm";
	
	private JoodlePanel jdlPanel;
	private JMenu fileMenu;
	private JMenu settMenu;
	private JMenu viewMenu;
	private JMenu helpMenu;
	
	private Action newAction;
	private Action loadAction;
	private Action saveAction;
	private Action exportAction;
	private Action exitAction;
	private Action aboutAction;
	private Action changeAction;
	private Action statusAction;
	private Action toolsAction;
	private Action helpAction;
	
	public JoodleFrame() {
		super("Joodle 1.0");

		jdlPanel = new JoodlePanel();
		setContentPane(jdlPanel);
		setJMenuBar(createMenuBar());
		setMinimumSize(new Dimension(700, 500));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setVisible(true);
		
	}
	/**
	 * Creates the menu of Joodle on which the "FILE", "SETTINGS" etc. menus will be placed
	 * @return a JMenuBar object representing the menu
	 */
	private JMenuBar createMenuBar() {
		JMenuBar mb = new JMenuBar();
		
		fileMenu = new JMenu(message("FILE"));
		
		newAction = new NewAction();
		fileMenu.add(newAction);
		fileMenu.addSeparator();
		loadAction = new SVGLoadAction();
		fileMenu.add(loadAction);
		saveAction = new SVGSaveAction();
		fileMenu.add(saveAction);		
		exportAction = new ImageSaveAction();
		fileMenu.add(exportAction);
		fileMenu.addSeparator();
		exitAction = new ExitAction();
		fileMenu.add(exitAction);
		mb.add(fileMenu);

		viewMenu = new JMenu(message("VIEW"));
		statusAction = new StatusbarAction(jdlPanel);
		JCheckBoxMenuItem viewCheck = new JCheckBoxMenuItem(statusAction);
		
		viewCheck.setSelected(true);
		viewMenu.add(viewCheck);

		toolsAction = new ToolbarAction(jdlPanel);
			
		JCheckBoxMenuItem toolCheck = new JCheckBoxMenuItem(toolsAction);
		toolCheck.setSelected(true);
		viewMenu.add(toolCheck);

		mb.add(viewMenu);
		
		settMenu = new JMenu(message("SETTINGS"));
		changeAction = new SettingsAction(this);
		settMenu.add(changeAction);
		mb.add(settMenu);
		
		helpMenu = new JMenu(message("HELP"));
		helpAction = new HelpAction();
		helpMenu.add(helpAction);
		aboutAction = new AboutAction(this); 
		helpMenu.add(aboutAction);
		mb.add(helpMenu);
		
		return mb;
	}
	
	/**
	 * Main Joodle panel. This can be added to a frame or an applet.
	 *
	 */
	class JoodlePanel extends JPanel implements SelectionObserver {
		JScrollPane tp = new JScrollPane(new ToolsPanel());
		JComponent cp = CanvasComponent.instance();
		JPanel ap = new AttributesPanel();
		JPanel sp = StatusPanel.instance();
		
		void toggleToolBarVisibility() {
			tp.setVisible(!tp.isVisible());
		}

		void toggleStatusBarVisibility() {
			sp.setVisible(!sp.isVisible());
		}
		
		JoodlePanel() {

			Canvas.instance().addSelectionObserver(this);
			
			GroupLayout layout = new GroupLayout(this);
			this.setLayout(layout);
			
			layout.setHorizontalGroup(
					layout.createParallelGroup().addGroup(
						layout.createSequentialGroup()
							.addComponent(tp, 100, 210, 210)
							.addGroup(layout.createParallelGroup()
									.addComponent(cp)
									.addComponent(ap)))
						.addComponent(sp, 0, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
						);
			layout.setVerticalGroup(
					layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup()
								.addComponent(tp)
								.addGroup(layout.createSequentialGroup()
										.addComponent(cp)
										.addComponent(ap))
										)
						.addComponent(sp, 0, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									);
			
			ap.setVisible(false);
		}

		/**
		 * Disables the attributes panel for composite shapes and
		 * when no shapes is selected
		 * @see SelectionObserver#selectionChanged(Shape s)
		 */
		@Override
		public void selectionChanged(Shape s) {
			ap.setVisible(s.isSelected() && !s.isComposite() 
					&& Canvas.instance().getSelectedShapes().size() == 1);
		}
	}
	
	/**
	 * Regenerates menu and action texts after language change.
	 */
	void resetTexts() {
		fileMenu.setText(message("FILE"));
		viewMenu.setText(message("VIEW"));
		settMenu.setText(message("SETTINGS"));
		helpMenu.setText(message("HELP"));
		
		newAction.putValue(Action.NAME, message("NEW"));
		loadAction.putValue(Action.NAME, message("LOAD"));
		saveAction.putValue(Action.NAME, message("SAVE"));
		exportAction.putValue(Action.NAME, message("EXPORT"));
		exitAction.putValue(Action.NAME, message("EXIT"));
		changeAction.putValue(Action.NAME, message("CHANGESETTINGS"));
		aboutAction.putValue(Action.NAME, message("ABOUT"));
		statusAction.putValue(Action.NAME, message("StatusBar"));
		toolsAction.putValue(Action.NAME, message("ToolsBar"));
		helpAction.putValue(Action.NAME, message("UserGuide"));
	}
	
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		}
		catch (Exception e) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}
			catch(Exception e2) {}
			setDefaultLookAndFeelDecorated(false);
		}

		try {
			Thread.sleep(1500);
		}
		catch(Exception e) {}
		
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new JoodleFrame();
			}
		});
	}
}
